package com.example.zkweixiu.app.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zkweixiu.app.R;
import com.example.zkweixiu.app.bean.ObjBean;
import com.example.zkweixiu.app.pro.PublicPro;
import com.google.gson.Gson;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxui.view.dialog.RxDialogShapeLoading;
import com.yanzhenjie.sofia.Sofia;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

// 密码修改- 记得原密码- 输入新密码
public class ChangePassword21Activity extends AppCompatActivity implements View.OnClickListener {
    private Intent intent;
    private RelativeLayout rl_change;
    private TextView tv_again;
    private EditText ed_data;
    private String code;
    private RxDialogShapeLoading rxDialogShapeLoading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password21);
        Sofia.with(ChangePassword21Activity.this).statusBarDarkFont().statusBarBackground(Color.parseColor("#FFFFFF"));

        rl_change = findViewById(R.id.rl_change);
        ImageView iv_fanhui = findViewById(R.id.iv_fanhui);
        View iv_fanhui2 = findViewById(R.id.iv_fanhui2);
        ed_data = findViewById(R.id.ed_data);
        //文字
        tv_again = findViewById(R.id.tv_again);

        iv_fanhui.setOnClickListener(this);
        iv_fanhui2.setOnClickListener(this);
        rl_change.setOnClickListener(this); //下一步

        code = getIntent().getStringExtra("code");
        rxDialogShapeLoading = new RxDialogShapeLoading(ChangePassword21Activity.this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_change:
                String trim = ed_data.getText().toString().trim();

                if (TextUtils.isEmpty(trim)) {
                    tv_again.setVisibility(View.VISIBLE);
                    Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (trim.length() <= 5 || trim.length() >= 13) {
                    tv_again.setVisibility(View.VISIBLE);
                    Toast.makeText(this, "密码长度不正确", Toast.LENGTH_SHORT).show();
                    return;
                }

                tv_again.setVisibility(View.INVISIBLE);

                rxDialogShapeLoading.show();
                if (TextUtils.isEmpty(code)) {

                    updataPass(trim);

                } else {
                    updataPass2(trim);
                }
//                intent = new Intent(this, ChangePassword21Activity.class);// // 密码修改- 记得原密码- 输入新密码 -完成
//                startActivity(intent);
                break;
            case R.id.iv_fanhui:
                finish();
                break;
            case R.id.iv_fanhui2:
                finish();
                break;

        }
    }

    /**
     * 密码修改
     *
     * @param trim
     */
    private void updataPass(String trim) {
        OkHttpUtils
                .get()
                .url(PublicPro.UPDATA_PASS)
                .addParams("pass", trim)
                .addParams("wx_uuid", PublicPro.userBean.getData().getGuaUuid())
                .addParams("count", "1")
                .addParams("token", PublicPro.userBean.getData().getGuaToken())
                .build()
                .execute(new StringCallback() {


                    @Override
                    public void onError(okhttp3.Call call, Exception e, int id) {
                        Log.e("HomeFragment1", e.getMessage());
                        if (rxDialogShapeLoading != null) {
                            rxDialogShapeLoading.dismiss();
                        }
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("HomeFragment1", response);

                        ObjBean objBean = new Gson().fromJson(response, ObjBean.class);
                        if (rxDialogShapeLoading != null) {
                            rxDialogShapeLoading.dismiss();
                        }
                        if (objBean.getStatus() == 200) {

                            tv_again.setVisibility(View.INVISIBLE);

                            Toast.makeText(ChangePassword21Activity.this, "修改成功", Toast.LENGTH_SHORT).show();
            /*                intent = new Intent(ChangePassword21Activity.this, ChangePassword21Activity.class);// // 密码修改- 记得原密码- 输入新密码
                            startActivity(intent);*/
                            RxActivityTool.finishActivity(ChangePassword2Activity.class);
                            finish();

                            return;
                        } else {
                            tv_again.setVisibility(View.VISIBLE);
                            Toast.makeText(ChangePassword21Activity.this, objBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }


    /**
     * 密码修改
     *
     * @param trim
     */
    private void updataPass2(String trim) {
        OkHttpUtils
                .get()
                .url(PublicPro.UPDATA_PASS)
                .addParams("pass", trim)
                .addParams("wx_uuid", PublicPro.userBean.getData().getGuaUuid())
                .addParams("count", "2")
                .addParams("code", code)
                .addParams("token", PublicPro.userBean.getData().getGuaToken())
                .build()
                .execute(new StringCallback() {


                    @Override
                    public void onError(okhttp3.Call call, Exception e, int id) {
                        Log.e("HomeFragment1", e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("HomeFragment1", response);

                        ObjBean objBean = new Gson().fromJson(response, ObjBean.class);

                        if (objBean.getStatus() == 200) {

                            tv_again.setVisibility(View.INVISIBLE);

                            Toast.makeText(ChangePassword21Activity.this, "修改成功", Toast.LENGTH_SHORT).show();
            /*                intent = new Intent(ChangePassword21Activity.this, ChangePassword21Activity.class);// // 密码修改- 记得原密码- 输入新密码
                            startActivity(intent);*/
                            RxActivityTool.finishActivity(ChangePassword3Activity.class);
                            finish();

                            return;
                        } else {
                            tv_again.setVisibility(View.VISIBLE);
                            Toast.makeText(ChangePassword21Activity.this, objBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }
}
